'''
Created on Jun 7, 2010

@author: pntruongan
'''

import os
import shutil

import codecs
'''
a = "/home/pntruongan/baitap/testDir"
files = os.listdir(a)
b = []
c = []
for f in files:
            
    fullpath = os.path.join(a, f)
    if os.path.isfile(fullpath):
        
        f1 = f.split('.')
        f2 = f1[0] + f1[1]
        if f2 not in b:
            b.append(f2)
            c.append(1)
        else:
            i = b.index(f2)
            c[i] += 1
print len(b)
for i in range(len(b)):
    print b[i], c[i]

#'''

'''
pwd = '/home/pntruongan/baitap/'

datadir = os.path.join(pwd, "testData")
testdir = os.path.join(pwd,"testDir") 

names = os.listdir( datadir)

count = 0

for name in names:
    subdir = os.path.join(datadir, name )
    
    subFiles = os.listdir(subdir)
    print subdir, len(subFiles)
    
    for i in range(len(subFiles)/3):
        newpath = os.path.join(testdir, name + '.' + subFiles[i])
        
        newFile = file(newpath, "w")
        
        
        newFile.write( codecs.open(os.path.join(subdir, subFiles[i]), "r","utf-16").read() )
        
        if (i+1 < len(subFiles)):
            newFile.write( codecs.open(os.path.join(subdir, subFiles[i+1]), "r","utf-16").read() )
        if (i+2 < len(subFiles)):
            newFile.write( codecs.open(os.path.join(subdir, subFiles[i+2]), "r","utf-16").read() )
        
        print newpath
        shutil.copy(os.path.join(subdir, i), os.path.join(testdir, name +'.' + i ) )
        print os.path.join(testdir, name +'.' + i )        

print count

#'''
'''
#dicIn = codecs.open(os.path.join( os.getcwdu(), 'vndics.txt'), "r", "utf-16")

dicIn = file(os.path.join( os.getcwdu(), 'vndics.txt'),"r")
dicOut = file(os.path.join( os.getcwdu(), 'vndicLower.txt'),"w")
#print dicIn.encoding
a = u""
a += dicIn.readline()
while a != "":
    a = a.lower()
    print a
    dicOut.write(a)
    a = u"" + dicIn.readline()
    
dicOut.close()
dicIn.close()

#'''

pwd = '/home/pntruongan/baitap/'

datadir = os.path.join(pwd, "testData")
testdir = os.path.join(pwd,"testDir") 

names = os.listdir( datadir)

count = 0

for name in names:
    subdir = os.path.join(datadir, name )
    
    subFiles = os.listdir(subdir)
    print subdir, len(subFiles)
    
    for i in subFiles:
        shutil.copy(os.path.join(subdir, i), os.path.join(testdir, name +'.' + i ) )
        print os.path.join(testdir, name +'.' + i )        

print count
